package com.itboye.sunsun.db;

import java.util.ArrayList;

import com.itboye.sunsun.beans.TieZiCaoGaoBean;
import com.itboye.sunsun.utils.Spanned2String;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
 * 保存草稿
 * @author admin
 *
 */
public class DBManager {

	DBHelper mDbHelper;
	SQLiteDatabase db;

	public DBManager(Context mContext) {
		mDbHelper = new DBHelper(mContext);
	}

	/***
	 * 添加进草稿
	 * @param uid
	 * @param title
	 * @param content
	 * @param time
	 * @param re
	 * @param img
	 * @return
	 */
	public long addNote(String uid,String title, String content,String img) {
		db = mDbHelper.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("uid", uid);
		cv.put("title", title);
		cv.put("content", content);
		cv.put("img", img);
		long num = db.insert("mynote", null, cv);
		db.close();
		return num;
	}
	

	/**
	 * 
	 * 
	 * @return
	 */
	public ArrayList<TieZiCaoGaoBean> queryNote(String uid) {
		ArrayList<TieZiCaoGaoBean> list = new ArrayList<TieZiCaoGaoBean>();
		db = mDbHelper.getReadableDatabase();

		Cursor mCursor = db.query("mynote", null, "uid=?", new String[]{uid}, null, null, null);
		if (mCursor != null) {
			while (mCursor.moveToNext()) {
				TieZiCaoGaoBean note = new TieZiCaoGaoBean();
				String title = mCursor.getString(mCursor
						.getColumnIndex("title"));
				note.setTitle(title);
				String content = mCursor.getString(mCursor
						.getColumnIndex("content"));
				String img = mCursor.getString(mCursor
						.getColumnIndex("img"));
				note.setId(uid);
				note.setTitle(title);
				note.setImg(img);
				note.setContent(content);
				list.add(note);
			}
			mCursor.close();
		}
		db.close();
		return list;
	}


	/**
	 * 删除缓存
	 * @param id
	 * @param title
	 * @param content
	 * @return
	 */
	public long delete(String uid) {
		db = mDbHelper.getWritableDatabase();
		long result=db.delete("mynote", "uid=?", new String[]{uid});
		return result;
	}
}
